/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conf2.ui;

import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author Administrator
 */
public class FrameUtil
{

    public static void bindKey(JFrame frame, KeyStroke key, Action action)
    {
        JPanel content = (JPanel) frame.getContentPane();
        bindKey(content, key, action);
    }

    public static void bindKey(JDialog dialog, KeyStroke key, Action action)
    {
        JPanel content = (JPanel) dialog.getContentPane();
        bindKey(content, key, action);
    }

    private static void bindKey(JPanel content, KeyStroke key, Action action)
    {
        InputMap im = content.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        final Object dummy = new Object();
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), dummy);

        ActionMap am = content.getActionMap();
        am.put(dummy, action);
    }
}
